package com.gusha.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysEnjoyDao;
import com.gusha.model.Project;
import com.gusha.model.SysArea;
import com.gusha.model.SysClassify;
import com.gusha.model.SysEnjoy;
import com.gusha.model.User;

@Service
@Transactional
public class SysEnjoyService {
	
    @Autowired
    private SysEnjoyDao sysEnjoyDao;
	
    public Page<SysEnjoy> findAll(Pageable pageable, SysEnjoy sysEnjoy) {
    	
        @SuppressWarnings("serial")
        Page<SysEnjoy> result= sysEnjoyDao.findAll(new Specification<SysEnjoy>() {
            @Override
            public Predicate toPredicate(Root<SysEnjoy> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if (sysEnjoy.getId() != null) {
                    list.add(cb.equal(root.get("id").as(BigInteger.class),sysEnjoy.getId()));
                }
                if (sysEnjoy.getEnName() != null) {
                    list.add(cb.equal(root.get("enName").as(String.class),sysEnjoy.getEnName()));
                }
                if (sysEnjoy.getEnDesc() != null) {
                    list.add(cb.equal(root.get("enDesc").as(String.class),sysEnjoy.getEnDesc()));
                }
                if (sysEnjoy.getEnContent()!=null) {
                    list.add(cb.equal(root.get("enContent").as(String.class),sysEnjoy.getEnContent()));
                }
                if (sysEnjoy.getAddTime()!=null) {
                    list.add(cb.equal(root.get("addTime").as(Date.class),sysEnjoy.getAddTime()));
                }
                if (sysEnjoy.getUpdateTime()!=null) {
                    list.add(cb.equal(root.get("updateTime").as(Date.class),sysEnjoy.getUpdateTime()));
                }
                if (sysEnjoy.getEnType()!=null) {
                    list.add(cb.equal(root.get("enType").as(BigInteger.class),sysEnjoy.getEnType()));
                }
                if (sysEnjoy.getIsDelete() != null) {
                    list.add(cb.equal(root.get("isDelete").as(BigInteger.class),sysEnjoy.getIsDelete()));
                }
                if (sysEnjoy.getEnPath() != null) {
                    list.add(cb.equal(root.get("enPath").as(String.class),sysEnjoy.getEnPath()));
                }
                if (sysEnjoy.getEnVideo() != null) {
                    list.add(cb.equal(root.get("enVideo").as(String.class),sysEnjoy.getEnVideo()));
                }
                if (sysEnjoy.getEnCheck() != null) {
                    list.add(cb.equal(root.get("enCheck").as(String.class),sysEnjoy.getEnCheck()));
                }
                if (sysEnjoy.getIsCheck()!= null) {
                    list.add(cb.equal(root.get("checkUser").as(String.class),sysEnjoy.getIsCheck()));
                }
                if (sysEnjoy.getEnUser() != null) {
                    list.add(cb.equal(root.get("enUser").as(String.class),sysEnjoy.getEnUser()));
                }
                if (sysEnjoy.getEnPrice() != null) {
                    list.add(cb.equal(root.get("enPrice").as(BigDecimal.class),sysEnjoy.getEnPrice()));
                }
                if (sysEnjoy.getEnArea() != null) {
                    list.add(cb.equal(root.get("enArea").as(BigInteger.class),sysEnjoy.getEnArea()));
                }
                if (sysEnjoy.getEnNumber()!= null) {
                    list.add(cb.equal(root.get("enNumber").as(BigInteger.class),sysEnjoy.getEnNumber()));
                }
                if (sysEnjoy.getEnHeft() != null) {
                    list.add(cb.equal(root.get("enHeft").as(String.class),sysEnjoy.getEnHeft()));
                }
                if (sysEnjoy.getEnProvince() != null) {
                    list.add(cb.equal(root.get("enProvince").as(SysArea.class),sysEnjoy.getEnProvince()));
                }
                if (sysEnjoy.getEnCity() != null) {
                    list.add(cb.equal(root.get("enCity").as(SysArea.class),sysEnjoy.getEnCity()));
                }
                if (sysEnjoy.getEnDistrict() != null) {
                    list.add(cb.equal(root.get("enDistrict").as(SysArea.class),sysEnjoy.getEnDistrict()));
                }
                if (sysEnjoy.getEnAddress() != null) {
                    list.add(cb.equal(root.get("enAddress").as(String.class),sysEnjoy.getEnAddress()));
                }
                if (sysEnjoy.getEnHealth() != null) {
                    list.add(cb.equal(root.get("enHealth").as(String.class),sysEnjoy.getEnHealth()));
                }
                
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
         return result;
    }

}
